package com.zigong.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.zigong.app.R;
import com.zigong.app.application.Constances;
import com.zigong.app.beans.ILiveDy;
import com.zigong.app.beans.ImageInfo;
import com.zigong.app.beans.LiveDyTitle;
import com.zigong.app.beans.LiveDynamic;
import com.zigong.app.ui.adapter.holder.BaseAbsHolder;
import com.zigong.app.ui.adapter.holder.LiveRoomDynamicHolder;
import com.zigong.app.ui.adapter.holder.OnlyTitleHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by rh008 on 2017/7/18.
 */

public class LiveRoomAdapter extends BaseHolderAbsAdapter<ILiveDy, BaseAbsHolder<ILiveDy>> {
    public static final int VIEWTYPE_DYNAMIC = 0;
    public static final int VIEWTYPE_COMPERE = 1;
    private ArrayList<LocalMedia> imags;

    private int comperePos = -1;

    public LiveRoomAdapter(Context context, List<ILiveDy> list) {
        super(context, list);
        imags = new ArrayList<>();
        hanleImages();
    }

    @Override
    public BaseAbsHolder<ILiveDy> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah;
        if (viewType == VIEWTYPE_COMPERE) {
            View v = LayoutInflater.from(context).inflate(R.layout.item_title_live_room, parent, false);
            bah = new OnlyTitleHolder(context, v);
        } else {
            View v = LayoutInflater.from(context).inflate(R.layout.item_live_room2, parent, false);
            bah = new LiveRoomDynamicHolder(context, v);
            bah.setInternalClick(this);
            bah.setOnImageClickListener(new BaseAbsHolder.OnImageClickListener() {
                @Override
                public void onClick(String imageUrl) {
                    clickImage(imageUrl);
                }
            });
        }
        return bah;
    }

    @Override
    public void onBindViewHolder(BaseAbsHolder<ILiveDy> holder, int position) {
        super.onBindViewHolder(holder, position);
        int mode = BaseAbsHolder.ITEMMODE_NORMAL;
        if (isReverse) {
            if (position == getItemCount() - 1) {
                mode = BaseAbsHolder.ITEMMODE_FIRST;
                if (position == 0) {
                    mode = BaseAbsHolder.ITEMMODE_SINGLE;
                }
            } else if (position == 0) {
                mode = BaseAbsHolder.ITEMMODE_LAST;
            }
        } else {
            if (position - comperePos == 1) {
                mode = BaseAbsHolder.ITEMMODE_FIRST;
                if (position == getItemCount() - 1) {
                    mode = BaseAbsHolder.ITEMMODE_SINGLE;
                }
            } else if (position == getItemCount() - 1) {
                mode = BaseAbsHolder.ITEMMODE_LAST;
            }
        }
        holder.setItemMode(mode);
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getDataViewType();
    }

    @Override
    public ILiveDy getItem(int position) {
        return super.getItem(position);
    }

    public LiveRoomAdapter setCompere(String compere) {
        LiveDyTitle liveDynamic = new LiveDyTitle();
        liveDynamic.setTitle(compere);
        liveDynamic.setType(VIEWTYPE_COMPERE);

        if (comperePos == 0) {
            data.remove(0);
        } else if (data == null) {
            data = new ArrayList<>();
        }
        data.add(0, liveDynamic);
        comperePos = 0;
        notifyItemChanged(0);
        return this;
    }

    public void addData(List<ILiveDy> dynamics) {
        if (imags == null) {
            imags = new ArrayList<>();
        }
        if (ListUtils.isListValued(dynamics)) {
            data.addAll(0, dynamics);
            notifyDataSetChanged();
            hanleImages();
        } else {
            return;
        }

    }

    public LiveRoomAdapter setDatas(List<? extends ILiveDy> data) {

        if (ListUtils.isListValued(data)) {
            ILiveDy liveDynamic = null;
            int i = 0;
            if (ListUtils.isListValued(this.data)) {
//                liveDynamic = this.data.get(0);
                this.data.clear();
            } else if (this.data == null) {
                this.data = new ArrayList<>();
            }

            if (liveDynamic != null) {
                this.data.add(liveDynamic);
            }
            this.data.addAll(data);
            if (imags == null) {
                imags = new ArrayList<>();
            }
            imags.clear();
            hanleImages();
            notifyDataSetChanged();
        }
        return this;
    }

    @Override
    public int addData(ILiveDy iLiveDy) {
        int i = super.addData(iLiveDy);
        if (i > 0) {
            notifyItemChanged(i - 1);
        }
        return i;
    }

    public LiveRoomAdapter setData(List<ILiveDy> data) {

        if (ListUtils.isListValued(data)) {
            ILiveDy liveDynamic = null;
            if (comperePos == 0 && ListUtils.isListValued(this.data)) {
                liveDynamic = this.data.get(0);
            }

            this.data = new ArrayList<>();
            if (liveDynamic != null) {
                this.data.add(liveDynamic);
            }
            this.data.addAll(data);
            if (imags == null) {
                imags = new ArrayList<>();
            }
            imags.clear();
            hanleImages();
            notifyDataSetChanged();
        }
        return this;
    }

    @Override
    public LiveRoomAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    private void hanleImages() {
        if (data != null && ListUtils.isListValued(data)) {
            for (ILiveDy dynamic : data) {
                List<ImageInfo> imag = dynamic.getContentImage();
                String content = dynamic.getContentText();
                if (ListUtils.isListValued(imag)) {
                    for (ImageInfo str : imag) {
                        if (!imags.contains(str)) {
                            String url = UrlUtils.linkUrls(Constances.ST_BASE_URL, str.getImageUrl());
                            LocalMedia localMedia = new LocalMedia();
                            localMedia.setPath(url);
                            localMedia.setDescribe(content);
                            imags.add(localMedia);
                        }
                    }
                }
            }
        }
    }

    private void clickImage(String url) {
        Intent intent = new Intent(context, PictureExternalPreviewActivity.class);
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, imags);
        for (int i = 0; i < imags.size(); i++) {
            String str = imags.get(i).getPath();
            if (str.contains(url)) {
                intent.putExtra(PictureConfig.EXTRA_POSITION, i);
            }
        }
        intent.putExtra("type", 1);
        context.startActivity(intent);
    }
}
